home *** CD-ROM | disk | FTP | other *** search
- #if !defined(USEDUMP)
- #include "lprlib.h"
- #include "lprdef.h"
- #include "lprfuncs.h"
- #else
- #pragma load "lprDumpFile"
- #endif
-
- #include "GetMyIPAddr.h"
- #include "AddressXlation.h"
-
- extern main(); /* for debugging */
- extern unsigned char tcpinitok; /* network code is active */
- extern unsigned char pend_conn; /* connection pending status */
- extern unsigned long * Ticks; /* tick count since Mac startup */
- extern unsigned long passtime; /* time password last stored */
- extern unsigned char * filelist; /* pointer to list of files to process */
- extern short currfnum; /* 0 or reference for open file */
- extern unsigned char prtstate; /* lpr protocol state */
- extern unsigned long prtstime; /* time prtstate last changed */
- extern unsigned char netstat[], connstat[], prtstat[];
- extern settings_list cs; /* current settings */
- extern short ioerror; /* result from tcp_init */
- extern domainresult dns; /* domain result structure */
- extern unsigned char queueflags[64]; /* flags for our queue */
- extern unsigned short queueevents[64]; /* corresponding events */
- unsigned char myipnum[4];
- static char tcpopnflg = 0;
- unsigned char * rcvbuff; /* stream buffer */
- unsigned long rcvlen; /* stream buffer length */
- unsigned char tcpmsg[256];
- short tcprefnum; /* ioRefNum from MacTCP open */
- union { /* IP address of remote host */
- unsigned long bin;
- unsigned char chr[4];
- } destaddr;
- StreamPtr mystream; /* pointer to our stream */
-
- void tcp_init(void)
- {
- pascal Boolean (*filterProc) ();
- static unsigned char nullstr[1] = {0};
- static unsigned char tcpname[] = "\p.IPP";
- IOParam openblk;
- struct IPParamBlock ipblk;
- TCPiopb cpb;
-
- sprintf(tcpmsg, "main at %lx", &main); /* in case we're using a map */
- putln(tcpmsg); /* for debugging */
-
- /* allocate buffers */
- rcvlen = 8192;
- rcvbuff = (unsigned char *)NewPtr(rcvlen);
- if (rcvbuff == 0L) {
- stgalert("tcp rcv buffer", "NewPtr", rcvlen);
- macend();
- ExitToShell();
- }
- /* here we open MacTCP */
- openblk.ioCompletion = 0;
- openblk.ioResult = 0;
- openblk.ioNamePtr = tcpname;
- openblk.ioRefNum = 0;
- openblk.ioPermssn = 0;
-
- ioerror = PBOpen((ParmBlkPtr)&openblk, 0);
-
- if (ioerror != 0) {
- resetcurs();
- filterProc = DlgFilter;
- sprintf(tcpmsg, "Error %d attempting to open MacTCP.", ioerror);
- c2pstr(tcpmsg);
- ParamText(tcpmsg, nullstr, nullstr, nullstr);
- p2cstr(tcpmsg);
- StopAlert(258, (ModalFilterProcPtr)filterProc);
- ParamText(nullstr, nullstr, nullstr, nullstr);
- return;
- }
-
- tcprefnum = openblk.ioRefNum;
-
- /* now open a stream to use */
- cpb.ioCRefNum = tcprefnum;
- cpb.csCode = TCPCreate;
- cpb.tcpStream = (StreamPtr)0;
- cpb.csParam.create.rcvBuff = rcvbuff;
- cpb.csParam.create.rcvBuffLen = rcvlen;
- cpb.csParam.create.notifyProc = (TCPNotifyProc)mynotify;
- cpb.csParam.create.userDataPtr = 0;
- ioerror = PBControl((ParmBlkPtr)&cpb, 0);
- if (ioerror != noErr) {
- resetcurs();
- filterProc = DlgFilter;
- sprintf(tcpmsg, "Error %d attempting to open new stream.", ioerror);
- c2pstr(tcpmsg);
- ParamText(tcpmsg, nullstr, nullstr, nullstr);
- p2cstr(tcpmsg);
- StopAlert(258, (ModalFilterProcPtr)filterProc);
- ParamText(nullstr, nullstr, nullstr, nullstr);
- return;
- }
- mystream = cpb.tcpStream;
-
- /* now open the name resolver */
-
- ioerror = OpenResolver((char *)0); /* use defaults hosts file location */
-
- if (ioerror != 0) {
- resetcurs();
- filterProc = DlgFilter;
- sprintf(tcpmsg, "Error %d attempting to open MacTCP name resolver.", ioerror);
- c2pstr(tcpmsg);
- ParamText(tcpmsg, nullstr, nullstr, nullstr);
- p2cstr(tcpmsg);
- StopAlert(258, (ModalFilterProcPtr)filterProc);
- ParamText(nullstr, nullstr, nullstr, nullstr);
- return;
- }
-
- ipblk.ioCompletion = 0;
- ipblk.ioResult = 0;
- ipblk.ioVRefNum = 0;
- ipblk.ioCRefNum = tcprefnum;
- ipblk.csCode = 15;
- ioerror = PBControl((ParmBlkPtr)&ipblk, 0);
- memcpy(myipnum, &(ipblk.ourAddress), 4);
- sprintf(netstat, "Registered as %d.%d.%d.%d",
- myipnum[0], myipnum[1], myipnum[2], myipnum[3]);
- statdlg(0, 0x01);
-
- tcpinitok = 1; /* remember to shutdown net stuff */
- }
-
- void tcplgin(void)
- {
- static struct hostInfo hinfo;
- short i;
- OSErr rc;
- ResultProcPtr rp;
-
- /* clear any previous queued events */
- for (i = 0; i < 64; i++) {
- queueflags[i] = 0;
- }
-
- hinfo.rtnCode = 0;
- strcpy(hinfo.cname, rdhostname());
- for (i=0; i < NUM_ALT_ADDRS; i++) {
- (hinfo.addr)[i] = 0;
- }
- rp = resolveproc;
- rc = StrToAddr(rdhostname(), &hinfo, rp, (char *)&dns);
- if (rc == cacheFault) {
- newconn(1);
- sprintf(connstat, "Getting address for %s", rdhostname());
- statdlg(0, 0x02);
- }
- else if (rc == noErr) {
- destaddr.bin = (hinfo.addr)[0];
- rc = netopen();
- if (rc != noErr) {
- prtclean();
- stoperr(lopnalrt);
- }
- }
- else { /* unexpected error */
- sprintf(tcpmsg, "Return code %d from StrToAddr for %s",
- rc, rdhostname());
- prtclean();
- stoperr(numalrt);
- }
- }
-
- pascal void resolveproc(hptr, userdata)
- struct hostInfo *hptr;
- char *userdata;
- {
- domainresult *dptr;
-
- dptr = (domainresult *)userdata;
- dptr->newresult = 1;
- dptr->rc = hptr->rtnCode;
- dptr->address = (hptr->addr)[0];
- }
-
- void tcpevent(void)
- {
- OSErr rc;
- unsigned short newevent;
-
- if (dns.newresult > 0) {
- dns.newresult = 0;
- if (pend_conn == 1) {
- if (dns.rc != noErr) {
- sprintf(tcpmsg, "Return code %d from StrToAddr for %s",
- dns.rc, rdhostname());
- newconn(0);
- prtclean();
- stoperr(numalrt);
- }
- else {
- destaddr.bin = dns.address;
- sprintf(tcpmsg, "Number for %s is %d.%d.%d.%d",
- rdhostname(), (destaddr.chr)[0], (destaddr.chr)[1],
- (destaddr.chr)[2], (destaddr.chr)[3]);
- putln(tcpmsg);
- rc = netopen();
- if (rc != noErr) {
- newconn(0);
- prtclean();
- stoperr(lopnalrt);
- }
- }
- }
- }
-
- newevent = getevent();
- while (newevent != 0) {
- sprintf(tcpmsg, "New event = %d", newevent);
- putln(tcpmsg);
- switch (newevent) {
- case TCPTerminate:
- case TCPClosing:
- tcplgout();
- break;
- case TCPULPTimeout:
- tcplgout();
- break;
- case TCPDataArrival:
- case TCPUrgent:
- tcpread();
- break;
- case TCPICMPReceived:
- default:
- break;
- }
- newevent = getevent();
- }
- }
-
- OSErr netopen(void)
- {
- OSErr rc;
- TCPiopb openpb;
- short pnum;
- /* use an available port in the lpr range */
- for (pnum=721; pnum <= 731; pnum++) {
- openpb.ioCRefNum = tcprefnum;
- openpb.csCode = TCPActiveOpen;
- openpb.tcpStream = mystream;
- openpb.csParam.open.ulpTimeoutValue = 0;
- openpb.csParam.open.ulpTimeoutAction = 0;
- openpb.csParam.open.validityFlags = 0;
- openpb.csParam.open.commandTimeoutValue = 0;
- openpb.csParam.open.remoteHost = destaddr.bin;
- openpb.csParam.open.remotePort = rdportnum();
- memcpy(&(openpb.csParam.open.localHost), myipnum, 4);
- openpb.csParam.open.localPort = pnum;
- openpb.csParam.open.tosFlags = 0;
- openpb.csParam.open.precedence = 0;
- openpb.csParam.open.dontFrag = false;
- openpb.csParam.open.timeToLive = 0;
- openpb.csParam.open.security = false;
- openpb.csParam.open.optionCnt = 0;
- openpb.csParam.open.userDataPtr = 0;
- rc = PBControl((ParmBlkPtr)&openpb, false);
- if (rc != duplicateSocket) break;
- }
- if (rc != noErr) return(rc);
- newconn(3);
- sprintf(connstat, "Connected to %s [%d.%d.%d.%d]",
- rdhostname(), (destaddr.chr)[0], (destaddr.chr)[1],
- (destaddr.chr)[2], (destaddr.chr)[3]);
- statdlg(0, 0x02);
- prtevent(0L, 0); /* start sending data */
- return(rc);
- }
-
- pascal void mynotify( streamPtr, code, uptr, terminReason, icmpMsg)
- StreamPtr streamPtr;
- unsigned short code;
- unsigned short terminReason;
- struct ICMPReport *icmpMsg;
- Ptr uptr; /* user data pointer */
- {
- #pragma unused(streamPtr, uptr, terminReason, icmpMsg)
- short i;
- char found;
-
- found = 0;
- for (i=0; i < 64; i++) {
- if (queueflags[i] == 0) {
- found = 1;
- break;
- }
- }
- if (!found) return;
- queueevents[i] = code;
- queueflags[i] = 1;
- }
-
- unsigned short getevent(void)
- {
- short i;
- char found;
- unsigned short newevent;
-
- found = 0;
- for (i=0; i < 64; i++) {
- if (queueflags[i] != 0) {
- found = 1;
- break;
- }
- }
- if (!found) return(0);
- newevent = queueevents[i];
- queueflags[i] = 0;
- return(newevent);
- }
-
- void tcpread(void)
- {
- static unsigned char readbuff[256];
- TCPiopb readpb;
- OSErr rc;
-
- readpb.ioCRefNum = tcprefnum;
- readpb.csCode = TCPRcv;
- readpb.tcpStream = mystream;
- readpb.csParam.receive.commandTimeoutValue = 0;
- readpb.csParam.receive.rcvBuff = readbuff;
- readpb.csParam.receive.rcvBuffLen = 256;
- readpb.csParam.receive.secondTimeStamp = 0;
- readpb.csParam.receive.userDataPtr = 0;
- rc = PBControl((ParmBlkPtr)&readpb, 0);
- if (rc != noErr) {
- return;
- }
- if (readpb.csParam.receive.rcvBuffLen > 0) {
- prtevent(readbuff, readpb.csParam.receive.rcvBuffLen);
- }
- }
-
- void tcpwrite(s, len)
- unsigned char *s;
- short len;
- {
- TCPiopb writepb;
- OSErr rc;
- struct {
- unsigned short count;
- unsigned char * addr;
- unsigned short end;
- } mywds;
-
- mywds.count = len;
- mywds.addr = s;
- mywds.end = 0;
-
- writepb.ioCRefNum = tcprefnum;
- writepb.csCode = TCPSend;
- writepb.tcpStream = mystream;
- writepb.csParam.send.ulpTimeoutValue = 0;
- writepb.csParam.send.ulpTimeoutAction = 0;
- writepb.csParam.send.validityFlags = 0;
- writepb.csParam.send.pushFlag = true;
- writepb.csParam.send.urgentFlag = false;
- writepb.csParam.send.wdsPtr = (Ptr)&mywds;
- writepb.csParam.send.sendFree = 0;
- writepb.csParam.send.sendLength = 0;
- writepb.csParam.send.userDataPtr = 0;
- rc = PBControl((ParmBlkPtr)&writepb, 0);
- if (rc != noErr) {
- tcplgout();
- stoperr(wrtalrt);
- }
- }
-
- void tcplgout(void)
- {
- TCPiopb closepb;
- OSErr rc;
-
- prtclean();
- if (pend_conn > 1) {
- closepb.ioCRefNum = tcprefnum;
- closepb.csCode = TCPClose;
- closepb.tcpStream = mystream;
- closepb.csParam.close.ulpTimeoutValue = 0;
- closepb.csParam.close.ulpTimeoutAction = 0;
- closepb.csParam.close.validityFlags = 0;
- closepb.csParam.close.userDataPtr = 0;
- rc = PBControl((ParmBlkPtr)&closepb, 0);
- newconn(0);
- }
- }
-
- void tcp_end(void)
- {
- OSErr rc;
- TCPiopb relpb;
-
- prtclean();
-
- if (tcpinitok) {
- rc = CloseResolver();
- tcpinitok = 0;
- }
-
- if (mystream != 0) {
- relpb.ioCRefNum = tcprefnum;
- relpb.csCode = TCPRelease;
- relpb.tcpStream = mystream;
- relpb.csParam.create.rcvBuff = rcvbuff;
- relpb.csParam.create.rcvBuffLen = rcvlen;
- rc = PBControl((ParmBlkPtr)&relpb, 0);
- mystream = 0;
- }
-
- if (rcvbuff != 0) {
- DisposPtr(rcvbuff);
- }
- }
-
- void prtclean(void)
- {
- resetcurs();
- prtstate = 0;
- prtstime = 0;
- if (currfnum != 0) {
- FSClose(currfnum);
- currfnum = 0;
- }
- if (filelist != 0) {
- DisposPtr(filelist);
- filelist = 0;
- }
- connstat[0] = prtstat[0] = 0;
- statdlg(0, 0x06);
- }
-
- void newconn(n) /* new pend_conn value is n */
- short n;
- {
- if (n == pend_conn) return;
- sprintf(tcpmsg, "pend_conn change from %d to %d",
- pend_conn, n);
- putln(tcpmsg);
- pend_conn = n;
- if (pend_conn == 0) passtime = (*Ticks);
- }
-
- unsigned char * rdhostname(void) /* return name part of cs.hostname string */
- {
- static unsigned char hname[128];
- register short i;
-
- memcpy(hname, cs.hostname, 128);
- for (i=0; i < 128; i++) {
- if (hname[i] == '\0') break;
- if (hname[i] == ':') {
- hname[i] = '\0';
- break;
- }
- }
- return(hname);
- }
-
- short rdportnum(void) /* return port number from cs.hostname, or default */
- {
- register short i, len, count, port;
- register unsigned char * s;
-
- len = strlen(cs.hostname);
- if (len < 2) return(HLPD); /* need at least colon and digit */
-
- count = 0;
- for (i=0; i < len; i++) {
- if (cs.hostname[i] == ':') {
- count++;
- break;
- }
- }
- if (count == 0) return(HLPD);
- s = cs.hostname + i + 1; /* s = host number string */
- if (s[0] == '\0') return(HLPD);
- port = atoi(s);
- if (port != 0) {
- sprintf(tcpmsg, "port number = %d", port);
- putln(tcpmsg);
- return(port);
- }
- else return(HLPD);
- }
-
- void putln (s) /* put message in error log */
- unsigned char * s;
- {
- FILE *fp;
-
- fp = dbgopen();
- if (fp == 0) return;
-
- fputs(s, fp);
- putc('\015', fp);
- if (cs.dblevel == 99) {
- putc('\012', fp);
- fflush(fp);
- dbgwait();
- }
-
- dbgclose(fp);
- }
-
- void tcpdump(msg, data, len)
- unsigned char *msg, *data;
- short len;
- {
- short count, i;
- FILE *fp;
-
- fp = dbgopen();
- if (fp == 0) return;
-
- fprintf(fp, "%s: %d bytes:", msg, len);
- putc('\015', fp);
- if (cs.dblevel == 99) {
- putc('\012', fp);
- fflush(fp);
- dbgwait();
- }
-
- count = 0;
- for (i=0; i < len; i++) {
- fprintf(fp, "%02x", data[i]);
- count++;
- if (count == 32) {
- putc('\015', fp);
- if (cs.dblevel == 99) {
- putc('\012', fp);
- fflush(fp);
- dbgwait();
- }
- count = 0;
- }
- }
- if (count != 0) {
- putc('\015', fp);
- if (cs.dblevel == 99) putc('\012', fp);
- }
- putc('\015', fp);
- if (cs.dblevel == 99) putc('\012', fp);
-
- dbgclose(fp);
- }
-
- FILE *dbgopen(void)
- {
- static FILE *fp;
- static char debugfile[] = "Ram:tcpdebug";
- static char debugser[] = ".aout";
-
- if (!cs.dblevel) return(0);
-
- if (cs.dblevel == 99) {
- if (tcpopnflg) return(fp);
- else {
- fp = fopen(debugser, "w");
- tcpopnflg = 1;
- return(fp);
- }
- }
-
- if (tcpopnflg)
- fp = fopen(debugfile, "a");
- else
- fp = fopen(debugfile, "w");
- if (fp != 0) tcpopnflg = 1;
- return(fp);
- }
-
- void dbgclose(fp)
- FILE *fp;
- {
- if (cs.dblevel == 99) return;
- fclose(fp);
- FlushVol("\pRam", 0);
- }
-
- void dbgwait(void)
- {
- static unsigned long *Ticks = 0x16a;
- unsigned long limit;
-
- limit = (*Ticks) + 15;
- while (limit > (*Ticks));
- }
-
-
-